package com.example.fang.demodruid.cocurrency6;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class MyTest1 {

    private Lock lock  =new ReentrantLock();

    public static void main(String[] args){
        MyTest1 myTest1=new MyTest1();
        IntStream.range(0,1000).forEach(i->{
             new Thread(()->{
                 myTest1.method();
                 //System.out.println("打印当前线程id:"+Thread.currentThread().getId()+"i是"+i);
             }).start();
        });

    }

    public void method(){
        try {
          lock.lock();
          try {
              Thread.sleep(1000);
          }catch (Exception e){
              e.printStackTrace();
          }
          System.out.println("method");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
